package com.pan.admin.module.loanProduct.service.impl;

import com.gitee.apanlh.util.valid.Assert;
import com.pan.admin.common.core.domain.entity.SysUser;
import com.pan.admin.common.exception.ServiceException;
import com.pan.admin.common.utils.SecurityUtils;
import com.pan.admin.module.loanProduct.dao.LoanProductDao;
import com.pan.admin.module.loanProduct.dao.LoanProductProtocolDao;
import com.pan.admin.module.loanProduct.entity.LoanProduct;
import com.pan.admin.module.loanProduct.entity.po.LoanProductProtocolPO;
import com.pan.admin.module.loanProduct.service.LoanProductProtocolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 产品协议Service业务层处理
 *
 * @author ZJQ
 * @date 2025-01-02
 */
@Service
public class LoanProductProtocolServiceImpl implements LoanProductProtocolService {

    @Autowired
    private LoanProductProtocolDao loanProductProtocolDao;

    @Autowired
    private LoanProductDao loanProductDao;

    /**
     * 检验用户权限
     *
     * @param id 产品信息主键
     * @return 结果
     */
    @Override
    public void checkDataScope(Long id) {
        if (!SysUser.isAdmin(SecurityUtils.getUserId())) {
            LoanProduct loanProduct = new LoanProduct();
            loanProduct.setId(id);
            LoanProduct loanProductId = loanProductDao.getByIdWithDataScope(loanProduct);
            if (loanProductId == null) {
                throw new ServiceException("没有权限访问用户数据！");
            }
        }
    }

    /**
     * 获取协议列表
     *
     * @param loanProductId 根据主体id，查找其协议
     */
    @Override
    @Transactional(readOnly = true)
    public List<LoanProductProtocolPO> listLoanProductProtocol(Long loanProductId) {
        return loanProductProtocolDao.listLoanProductProtocol(loanProductId);
    }

    /**
     * 获取协议
     *
     * @param id 根据协议id，获取协议具体内容
     */
    @Override
    @Transactional(readOnly = true)
    public LoanProductProtocolPO getLoanProductProtocol(Long id) {
        return loanProductProtocolDao.getById(id);
    }

    /**
     * 获取主体id
     *
     * @param id 根据协议id，获取主体id
     */
    @Override
    @Transactional(readOnly = true)
    public Long getLoanProductId(Long id) {
        return loanProductProtocolDao.getLoanProductId(id);
    }

    /**
     * 更新协议
     *
     * @param loanProductProtocolPO
     */
    @Override
    @Transactional
    public int updateLoanProductProtocol(LoanProductProtocolPO loanProductProtocolPO) {
        Assert.isNotNull(loanProductProtocolPO);
        loanProductProtocolPO.setUpdateBy(SecurityUtils.getUsername());
        return loanProductProtocolDao.updateLoanProductProtocol(loanProductProtocolPO);
    }

    /**
     * 删除协议
     *
     * @param id 根据协议id 删除单条
     */
    @Override
    @Transactional
    public int deleteLoanProductProtocol(Long id) {
        return loanProductProtocolDao.deleteById(id);
    }

    /**
     * 新增协议
     *
     * @param loanProductProtocolPO
     */
    @Override
    @Transactional
    public int insertLoanProductProtocol(LoanProductProtocolPO loanProductProtocolPO) {
        Assert.isNotNull(loanProductProtocolPO);
        loanProductProtocolPO.setCreateBy(SecurityUtils.getUsername());
        loanProductProtocolPO.setUpdateBy(SecurityUtils.getUsername());
        return loanProductProtocolDao.insertLoanProductProtocol(loanProductProtocolPO);
    }

}
